package com.ifp.opengate.boot.utils;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Classname LoadBalanceUtils
 * @Description TODO
 * @Date 2019/7/17 15:14
 * @Created by zzh
 *          <p>
 *          zhuzh@belink.com
 *          </p>
 * @Version 1.0
 */
public class LoadBalanceUtils {

    /**
     * 随机算法
     *
     * @return
     */
    public static String RoundRobin(Map<Object, Object> connectorMap) {
        Map<Object, Object> ipServerMap = new ConcurrentHashMap<>();
        ipServerMap.putAll(connectorMap);
        // 2.取出来key,放到set中
        Set<Object> ipset = ipServerMap.keySet();
        // 3.set放到list，要循环list取出
        ArrayList<Object> iplist = new ArrayList<Object>();
        iplist.addAll(ipset);
        // 循环随机数
        Random random = new Random();
        // 随机数在list数量中取（1-list.size）
        int pos = random.nextInt(iplist.size());
        return (String)iplist.get(pos);
    }

}
